#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "rtk_api_ext.h"
#include <libmdionetlink/mdio_api.h>

#if 1
char *bus="stmmac-1";
uint16_t gdev = 0x18;
#else
char *bus="stmmac-2";
uint16_t gdev = 0x18;
#endif

int32 g9x_eth1_read_phy_reg(uint8 phyID, uint8 regAddr, uint16 *data)
{
	return mdio_read_reg(bus, phyID, regAddr, data);
}

int32 g9x_eth1_write_phy_reg(uint8 phyID, uint8 regAddr, uint16 data)
{
	return mdio_write_reg(bus, phyID, regAddr, data);
}

int checkCPU()
{
    union w {
        int  a;
        char b;
    } c;
    c.a = 1;
    /* ==1, is little ending 
     * ==0, is Big ending 
     * */
    return (c.b == 1);
}

int main(int argc, char **argv)
{
	uint32_t chipVal;
	uint16 portmask;
	rtk_api_ret_t ret;
    uint8 portid;
	
	if (mdio_init()) {
		if (mdio_modprobe()) {
			fprintf(stderr, "ERROR: mdio-netlink module not "
				"detected, and could not be loaded.\n");
			return 1;
		}

		if (mdio_init()) {
			fprintf(stderr, "ERROR: Unable to initialize.\n");
			return 1;
		}
	}

	RTL906X_MDCMDIO_init(g9x_eth1_write_phy_reg, g9x_eth1_read_phy_reg);
	rtk_sys_CHIPID_get(&chipVal);
	printf("chipVal: 0x%4.4x\n", chipVal);

	rtk_sys_init();
	if(argc == 1){
 		printf("please use rtl906xCtrl eraseall to erase spi flash\n");
    printf("\trtl906xCtrl enablerwake to enable remote wakeup dection\n");
    printf("\trtl906xCtrl disablerwake to disable remote wakeup dection\n");
    printf("\trtl906xCtrl globalsleep to set global state as ST_SLEEP\n");
    printf("\trtl906xCtrl acceptsleeprequest to set all ports accept sleep request from link partner\n");
    printf("\trtl906xCtrl rejectsleeprequest to set all ports reject sleep request from link partner\n");
    printf("\trtl906xCtrl enablewholechipsleep to enable the whole chip sleep function\n");
    printf("\trtl906xCtrl disablewholechipsleep to disable the whole chip sleep function\n");
    printf("\trtl906xCtrl state to show global state\n");
    printf("\trtl906xCtrl wakeflagclearall to clear all wake flag\n");
    printf("\trtl906xCtrl phyreset to do phy reset\n");
    printf("\n");
    printf("\trtl906xCtrl portmask sendrwake to send WUR/WUP tp link partner\n");
    printf("\trtl906xCtrl portmask sendsleeprequest to send sleep request to link partner\n");
    printf("\trtl906xCtrl portmask forwardsleeprequest to forward sleep request to link partner\n");
    printf("\trtl906xCtrl portmask wholechipsleepmsk to forward sleep request to link partner\n");
    printf("\n");
    printf("\trtl906xCtrl port localstate to get port's local state\n");
    printf("\n");
    printf("\trtl906xCtrl phyregget [phyid] [reg] to get external phy reg\n");
    printf("\n");
    printf("\trtl906xCtrl phyregset [phyid] [reg] [data] to set external phy reg\n");
 		/*rtk_spiflash_init();
 		rtk_spiflash_eraseall();*/
	}else if(argc == 2){
    if(memcmp(argv[1],"enablerwake",strlen("enablerwake")) == 0){
			portmask = 0x6ff;
      printf("will do rtk_phy_sleep_cap_set(0x6ff, 1)\n");
      ret = rtk_phy_sleep_cap_set(portmask, 1);
      printf("ret:%i\n",ret);
			printf("will do rtk_op_disable_rwake_set(0x6ff, 0)\n");
			ret = rtk_op_disable_rwake_set(portmask, 0);
			printf("ret:%i\n",ret);
		}else if(memcmp(argv[1],"disablerwake",strlen("disablerwake")) == 0){
			portmask = 0x6ff;
			printf("will do rtk_op_disable_rwake_set(0x6ff, 1)\n");
			ret = rtk_op_disable_rwake_set(portmask, 1);
			printf("ret:%i\n",ret);
		}else if(memcmp(argv[1],"eraseall",strlen("eraseall")) == 0){
			printf("will do rtk_spiflash_eraseall\n");
 		  rtk_spiflash_init();
 		  rtk_spiflash_eraseall();
		}else if(memcmp(argv[1],"globalsleep",strlen("globalsleep")) == 0){     
			printf("will do rtk_op_glob_cmd(0x40)\n");
 		  ret = rtk_op_glob_cmd(0x40);
      printf("ret:%i\n",ret);
		}else if(memcmp(argv[1],"acceptsleeprequest",strlen("acceptsleeprequest")) == 0){
      portmask = 0x6ff;
      printf("will do rtk_phy_sleep_cap_set(0x6ff, 1)\n");
      ret = rtk_phy_sleep_cap_set(portmask, 1);
      printf("ret:%i\n",ret);
      printf("will do rtk_op_AccpetSleepRequest_cap_set(0x6ff, 1)\n");
 		  ret = rtk_op_AccpetSleepRequest_cap_set(portmask, 1);
      printf("ret:%i\n",ret);
		}else if(memcmp(argv[1],"rejectsleeprequest",strlen("rejectsleeprequest")) == 0){
      portmask = 0x6ff;
			printf("will do rtk_op_AccpetSleepRequest_cap_set(0x6ff, 0)\n");
 		  ret = rtk_op_AccpetSleepRequest_cap_set(portmask, 0);
      printf("ret:%i\n",ret);
		}else if(memcmp(argv[1],"enablewholechipsleep",strlen("enablewholechipsleep")) == 0){
			printf("will do rtk_op_WholechipSleepEnable_set(1)\n");
 		  ret = rtk_op_WholechipSleepEnable_set(1);
      printf("ret:%i\n",ret);
		}else if(memcmp(argv[1],"disablewholechipsleep",strlen("disablewholechipsleep")) == 0){
			printf("will do rtk_op_WholechipSleepEnable_set(0)\n");
 		  ret = rtk_op_WholechipSleepEnable_set(0);
      printf("ret:%i\n",ret);
		}else if(memcmp(argv[1],"state",strlen("state")) == 0){
			uint16 state;
      rtk_op_global_state_get(&state);
      printf("rtk_op_global_state_get state:0x%x\n", state);
		}else if(memcmp(argv[1],"wakeflagclearall",strlen("wakeflagclearall")) == 0){
      printf("will do rtk_op_wakeflag_clearall()\n");
      ret = rtk_op_wakeflag_clearall();
      printf("ret:%i\n",ret);
		}else if(memcmp(argv[1],"phyreset",strlen("phyreset")) == 0){
      printf("will do rtk_phy_reset(0x3ff)\n");
      ret = rtk_phy_reset(0x3ff);
      printf("ret:%i\n",ret);
		}else{
      printf("no this subcommand:%s\n", argv[1]);
    }
	}else if(argc == 3){
     int mask=0;
     sscanf(argv[1], "%x", &mask);
     portmask = mask;
     printf("portmask:%x\n", portmask);
     if(memcmp(argv[2],"sendrwake",strlen("sendrwake")) == 0){
			printf("will do rtk_op_en_wupr_tx_set(0x%x, 1)\n", portmask);
			ret = rtk_op_en_wupr_tx_set(portmask, 1);
			printf("ret:%i\n",ret);
			printf("will do rtk_op_port_send_rwake(0x%x)\n", portmask);
			ret = rtk_op_port_send_rwake(portmask);
			printf("ret:%i\n",ret);
		}else if(memcmp(argv[2],"sendsleeprequest",strlen("sendsleeprequest")) == 0){
      printf("will do rtk_phy_sleep_cap_set(0x%x, 1)\n", portmask);
      ret = rtk_phy_sleep_cap_set(portmask, 1);
      printf("ret:%i\n",ret);
			printf("will do rtk_op_port_send_SleepRequest(0x%x)\n", portmask);
 		  ret = rtk_op_port_send_SleepRequest(portmask);
      printf("ret:%i\n",ret);
		}else if(memcmp(argv[2],"forwardsleeprequest",strlen("forwardsleeprequest")) == 0){
      printf("will do rtk_phy_sleep_cap_set(0x%x, 1)\n", portmask);
      ret = rtk_phy_sleep_cap_set(portmask, 1);
      printf("ret:%i\n",ret);
			printf("will do rtk_op_Txsleepportmsk_set(0x%x, 1)\n", portmask);
 		  ret = rtk_op_Txsleepportmsk_set(portmask, 1);
      printf("ret:%i\n",ret);
		}else if(memcmp(argv[2],"wholechipsleepmsk",strlen("wholechipsleepmsk")) == 0){
      printf("will do rtk_op_WholechipSleepmsk_set(0x6ff, 0)\n");
      ret = rtk_op_WholechipSleepmsk_set(0x6ff, 0x0);
      printf("ret:%i\n",ret);
      printf("will do rtk_op_WholechipSleepmsk_set(0x%x, 1)\n", portmask);
      ret = rtk_op_WholechipSleepmsk_set(portmask, 0x1);
      printf("ret:%i\n",ret);
      ret = rtk_op_WholechipSleepmsk_get(&portmask);
      printf("rtk_op_WholechipSleepmsk_get ret:%i portmask:0x%x\n", ret, portmask);
    }
    else if(memcmp(argv[2],"localstate",strlen("localstate")) == 0){
      uint16 state;
      portid = portmask;
      printf("portid:%i\n", portid);
      printf("will do rtk_op_local_status_get(%i, &state)\n", portid);
      ret = rtk_op_local_status_get(portid, &state);
      printf("ret:%i state:0x%x\n", ret, state);
    }else{
      printf("no this port subcommand:%s\n", argv[2]);
    }
  }else if(argc == 4){
    int phyid=0;
    int reg=0;
    uint16 data=0;
    sscanf(argv[2], "%x", &phyid);
    sscanf(argv[3], "%x", &reg);
    if(memcmp(argv[1],"phyregget",strlen("phyregget")) == 0){
      printf("phyid:0x%x\n", phyid);
      printf("reg:0x%x\n", reg);
      printf("will do rtk_std_mdx_init & rtk_std_mdx_register_get\n");
      ret = rtk_std_mdx_init((rtk_mdx_baudrate_t)5u, (rtk_mdx_preamble_t)RTK_MDX_PREAMBLE_32);
      ret = rtk_std_mdx_register_get(phyid, reg, &data);
      printf("ret:%i data:0x%x\n", ret, data);
    }else{
      printf("no this subcommand:%s\n", argv[1]);
    }
  }else if(argc == 5){
    int phyid=0;
    int reg=0;
    int data=0;
    uint16 getdata=0;
    sscanf(argv[2], "%x", &phyid);
    sscanf(argv[3], "%x", &reg);
    sscanf(argv[4], "%x", &data);
    if(memcmp(argv[1],"phyregset",strlen("phyregset")) == 0){
      printf("phyid:0x%x\n", phyid);
      printf("reg:0x%x\n", reg);
      printf("will do rtk_std_mdx_init & rtk_std_mdx_register_set\n");
      ret = rtk_std_mdx_init((rtk_mdx_baudrate_t)5u, (rtk_mdx_preamble_t)RTK_MDX_PREAMBLE_32);
      ret = rtk_std_mdx_register_set(phyid, reg, data);
      ret = rtk_std_mdx_register_get(phyid, reg, &getdata);
      printf("ret:%i getdata:0x%x\n", ret, getdata);
    }else{
      printf("no this subcommand:%s\n", argv[1]);
    }
  }
   
}




